#include "game.h"


void menu1()
{
	printf("*************************\n");
	printf("*******  1. play  *******\n");
	printf("*******  0. exit  *******\n");
	printf("*************************\n");
}

void menu2()
{
	printf("*****************************\n");
	printf("*******  1. 简单难度  *******\n");
	printf("*******  2. 普通难度  *******\n");
	printf("*******  3. 困难难度  *******\n");
	printf("*******  4. 自定义难度 ******\n");
	printf("*****************************\n");
}


//通过动态内存创建mine数组，并初始化为 '0'  
char** mine_creat(int x, int y)
{
	char** mine = (char**)malloc(x * sizeof(char*));
	for (int i = 0; i < x; i++)
	{
		mine[i] = (char*)malloc(y * sizeof(char));
		for (int j = 0; j < y; j++)
		{
			mine[i][j] = '0';
		}
	}
	return mine;
}

//通过动态内存创建show数组 ，并初始化为 '0'   
char** show_creat(int x, int y)
{
	char** show = (char**)malloc(x * sizeof(char*));
	for (int i = 0; i < x; i++)
	{
		show[i] = (char*)malloc(y * sizeof(char));
		for (int j = 0; j < y; j++)
		{
			show[i][j] = '0';
		}
	}
	return show;
}


//初始化数组
void InitBoard(char** board, int rows, int cols, char set)
{
	int i = 0;
	for (i = 1; i < rows; i++)
	{
		int j = 0;
		for (j = 1; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}


//打印棋盘
void DisplayBoard(char** board, int rows, int cols)
{
	printf("---------扫雷游戏---------\n");
	int i = 0;
	for (i = 0; i < cols - 1; i++)
	{
		if(i < 10)
		{
			printf("%d  ", i);
		}
		else
		{
			printf("%d ", i);
		}
	}
	printf("\n");
	for (i = 1; i < rows - 1; i++)
	{
		if(i < 10)
		{
			printf("%d  ", i);
		}
		else
		{
			printf("%d ", i);
		}
		int j = 0;
		for (j = 1; j < cols - 1; j++)
		{
			printf("%c  ", board[i][j]);
		}
		printf("\n");
	}
}


//布置雷
void SetMine(char** mine, int rows, int cols, int count)
{
	int mine_count = count;
	
	while(mine_count)
	{
		int x = rand() % (rows - 1);
		int y = rand() % (cols - 1);
		
		if (x >= 1 && x <= (rows - 1) && y >= 1 && y <= (cols - 1))
		{
			if (mine[x][y] == '0')
			{
				mine[x][y] = '1';
				mine_count--;
			}
		}
	}
}


//标记雷的位置
void MarkMine(char** show, int rows, int cols)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("\n输入要标记的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= (rows - 1) && y >= 1 && y <= (cols - 1))
		{
			if (show[x][y] == '*')
			{
				show[x][y] = '!';
				rewind(stdin);
				system("cls");
				DisplayBoard(show, rows, cols);
				break;
			}
			else
			{
				printf("该位置不能被标记\n");
			}
		}
		else
		{
			printf("坐标错误，请重新输入\n");
		}
	}
}


//计算周围雷的个数
int GetMineCount(char** mine, int rows, int cols, int x, int y)
{
	int i = 0;
	int j = 0;
	int count = 0;
	for(i = x - 1; i <= x + 1; i++)
	{
		for(j = y - 1; j <= y + 1; j++)
		{
			if(i >= 0 && i < rows && j >= 0 && j < cols && mine[i][j] == '1')
			{
				count++;
			}
		}
	}
	return count;
}


//实现扫雷的递归展开
void OpenBoard(char** mine, char** show, int rows, int cols, int x, int y)
{
	int i = 0;
	int j = 0;
	int count = GetMineCount(mine, rows, cols, x, y);
	if(count == 0)
	{
		show[x][y] = ' ';
		for(i = x - 1; i <= x + 1; i++)
		{
			for(j = y - 1; j <= y + 1; j++)
			{
				if(i > 0 && j > 0 && i < rows - 1 && j < cols - 1 && (show[i][j] == '*') && (show[i][j] != '!') && (mine[i][j] != '1'))
				{
					OpenBoard(mine, show, rows, cols, i, j);
				}
			}
		}
	}
	else
	{
		show[x][y] = count + '0';
	}
}


//计算未被排出的雷
int NumberMine(char** show, int rows, int cols)
{
	int i = 0;
	int j = 0;
	int count = 0;
	for (i = 1; i < rows - 1; i++)
	{
		for (j = 1; j < cols - 1; j++)
		{
			if(show[i][j] == '*' || show[i][j] == '!')
			{
				count++;
			}
		}
	}
	return count;
}



//排查雷
void FindMine(char** mine, char** show, int rows, int cols, int count)
{
	int x = 0;
	int y = 0;
	int ch = 0;
	int number = NumberMine(show, rows, cols);
	while (number > count)
	{
		printf("请输入排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= (rows - 1) && y >= 1 && y <= (cols - 1))
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾，你被炸死了\n");
				DisplayBoard(mine, rows, cols);
				break;
			}
			else
			{
				if (show[x][y] != '!')
				{
					OpenBoard(mine, show, rows, cols, x, y);
					system("cls");
					DisplayBoard(show, rows, cols);	
				}
				else
				{
					printf("坐标已被标记，请重新输入\n");
				}
				printf("需要标记雷的位置按0,按其他键取消:>");
				scanf("%d", &ch);
				rewind(stdin);
				if (ch == 0)
				{
					MarkMine(show, rows, cols);
					number = NumberMine(show, rows, cols);
					if (number == count)
						break;
				}
				else
				{
					continue;
				}
				number = NumberMine(show, rows, cols);
			}
		}
		else
		{
			printf("坐标错误，请重新输入\n");
		}
	}
	if (number == count)
	{
		printf("恭喜你，排雷成功\n");
	}
}
